import { _decorator, Collider2D, Contact2DType, IPhysics2DContact, math, Node, PhysicsSystem2D, RigidBody2D, Vec2, Vec3 } from 'cc';
import { BaseEntity } from './BaseEntity';
import { Player } from './Player';
import { CacheManager } from '../../manager/CacheManager';

const { ccclass, property } = _decorator;

@ccclass('Bullet')
export class Bullet extends BaseEntity {
    @property(RigidBody2D)
    rigidbody: RigidBody2D = null;

    @property(Collider2D)
    collider: Collider2D = null;

    @property
    bulletSpeed: number = 500;

    @property
    damage: number = 50;

    @property
    lifeTime: number = 3; // 子弹最大存活时间

    @property
    trackingDuration: number = 0.5; // 追踪持续时间
    
    @property
    trackingStrength: number = 0.1; // 追踪强度(0-1)

    private _timer: number = 0;
    private _trackingTimer: number = 0;
    private _initialDirection: Vec2 = new Vec2();
    private _player: Node = null;

    protected onLoad(): void {
        super.onLoad();
        this.collider.on(Contact2DType.BEGIN_CONTACT, this.onCollisionEnter, this);
        this._player = CacheManager.game.player.node;
    }

    public init() {
        this.node.active = true;
        this.rigidbody.enabled = true;
        this.collider.enabled = true;
        this._timer = 0;
        this._trackingTimer = 0;
    }

    public shoot(startPos: Vec3, direction: Vec2) {
        this.node.setWorldPosition(startPos);
        this._initialDirection = direction.clone();
        this.rigidbody.linearVelocity = direction.multiplyScalar(this.bulletSpeed);
    }

    update(deltaTime: number) {
        // 超时自动销毁
        this._timer += deltaTime;
        if (this._timer >= this.lifeTime) {
            this.destroyBullet();
            return;
        }

        // 追踪逻辑
        if (this._trackingTimer < this.trackingDuration && this._player && this._player.active) {
            this._trackingTimer += deltaTime;
            
            // 计算朝向玩家的方向
            const bulletPos = this.node.getWorldPosition();
            const playerPos = this._player.getWorldPosition();
            const toPlayer = new Vec2(
                playerPos.x - bulletPos.x,
                playerPos.y - bulletPos.y
            ).normalize();

            // 混合原始方向和追踪方向
            const blendedDirection = new Vec2(
                math.lerp(this._initialDirection.x, toPlayer.x, this.trackingStrength),
                math.lerp(this._initialDirection.y, toPlayer.y, this.trackingStrength)
            ).normalize();

            // 更新子弹速度
            this.rigidbody.linearVelocity = blendedDirection.multiplyScalar(this.bulletSpeed);
        }
    }

    private onCollisionEnter(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact) {
        const player = otherCollider.getComponent(Player);
        if (player) {
            // 这里可以触发玩家受伤逻辑
            CacheManager.game.playerVo.hp -= this.damage;
            // player.takeDamage(this.damage);
            this.destroyBullet();
        }
    }

    private destroyBullet() {
        // 这里可以添加销毁特效
        this.reset();
    }

    public reset(): void {
        this.rigidbody.linearVelocity = Vec2.ZERO;
        this.rigidbody.enabled = false;
        this.collider.enabled = false;
        this.node.active = false;
        super.reset();
    }
} 